var index_ligne = 0;
var prelev_type = '';

function init(){
   document.saisie_prelev.liste_mois.options[0].selected = true;
   document.saisie_prelev.cotiz_std.value = "";
   document.saisie_prelev.prelev_type.checked = true;
}

function ajoutLigneSaisie(){
	index_ligne = index_ligne + 1;

	var elem = document.getElementById("cotizSpecific");
	var newDiv = document.createElement("p");
	var ligneHtml = "Cotisation (specifique): <input id=\"spec_!CODE_cotiz\" type=\"text\"/> EUR, Association Dept.: <input id=\"spec_!CODE_ad\" type=\"text\"/> <img onclick=\"ajoutLigneSaisie()\" src=\"images/add.gif\" alt=\"+\"/>";

	if ( index_ligne < 10 ){ligneHtml = ligneHtml.replace(/!CODE/g, "0" + index_ligne);}
	if ( 9 < index_ligne < 100 ){ligneHtml = ligneHtml.replace(/!CODE/g, "" + index_ligne);}

   if (index_ligne == 1){
      var elem2 = document.getElementById("marqueur_specific");
      elem.removeChild(elem2);
      newDiv.innerHTML = ligneHtml; 
      elem.appendChild(newDiv);
   }
   else {
   	newDiv.innerHTML = ligneHtml; elem.appendChild(newDiv);
   }
}

function validationFormulaire() {
   var b_valide = true;
   var m_tableau = new Array();
   var standard = new Array (2);

   standard[0] = document.saisie_prelev.liste_mois.value;
   standard[1] = document.saisie_prelev.cotiz_std.value;

   if ( chaineEstNumeric(standard[1]) )   {
      standard[1] = formatageNumeric(standard[1]);
      m_tableau[0] = standard;
      document.saisie_prelev.cotiz_std.className = "normal";
   }
   else   {
      document.saisie_prelev.cotiz_std.className = "error";
      b_valide = false;
   }

   var p_cotiz; var p_asso;
   var o_cotiz; var o_asso;

   for ( i=1; i<index_ligne + 1; i++ )   {
      var s_tableau = new Array(2);
      if ( i < 10 ) {
         p_cotiz = "spec_0"+ i + "_cotiz"; p_asso = "spec_0"+ i + "_ad";
      }
      if( 9 < i && i < 100)	   {
         p_cotiz = "spec_" + i + "_cotiz"; p_asso = "spec_" + i + "_ad";
	   }
      o_cotiz=document.getElementById(p_cotiz);
      o_asso =document.getElementById(p_asso);

      s_tableau[0]=o_cotiz.value;
      s_tableau[1]=o_asso.value;

      if ( chaineEstNumeric( s_tableau[0] )){
         o_cotiz.className = "normal"; 
         s_tableau[0] = formatageNumeric(s_tableau[0]);
      }
      else{
         o_cotiz.className = "error";
         b_valide = false;
      }
      if ( chaineEstAsso( s_tableau[1] )){
      o_asso.className = "normal";
      }
      else {
         o_asso.className = "error";
         b_valide = false;
      }
      m_tableau[i]=s_tableau;
   }
   
   if (b_valide) {
	  /*
	  var elem = document.getElementById("debug_output");
      var elem2 = document.getElementById("debug_output").firstChild;
      if (elem2 != null){
         elem.removeChild(elem2);
      }
	   var newElem = document.createElement("span");
      newElem.innerHTML = JSON.stringify(m_tableau);
	   elem.appendChild(newElem);
	   */
	   send_request(JSON.stringify(m_tableau));
   }
}

function chaineEstNumeric(chaine){
   re = /^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[0-9],[1-9]|[1-9][0-9],[0-9]|[1-9][0-9][0-9],[0-9]|[0-9],[0-9][0-9]|[1-9][0-9],[0-9][0-9]|[1-9][0-9][0-9],[0-9][0-9])$/
   return re.test(chaine);
}
function chaineEstAsso(chaine){
   re = /^([0-9][0-9]|[0-9])$/
   return re.test(chaine);
}

function formatageNumeric(numeric){
   var chaine = "";

   var re = /^[1-9]$/;
   if ( re.test(numeric)){ chaine = "0000" + numeric + "00"; }

   re = /^[1-9][0-9]$/
   if ( re.test(numeric)){ chaine = "000" + numeric + "00"; }

   re = /^[1-9][0-9][0-9]$/
   if ( re.test(numeric)){ chaine = "00" + numeric + "00"; }

   re = /^[0-9],[1-9]$/
   if ( re.test(numeric)){ chaine = "0000" + numeric + "0"; }

   re = /^[1-9][0-9],[0-9]$/
   if ( re.test(numeric)){ chaine = "000" + numeric + "0"; }
   
   re = /^[1-9][0-9][0-9],[0-9]$/
   if ( re.test(numeric)){ chaine = "00" + numeric + "0"; }

   re = /^[0-9],[0-9][0-9]$/
   if ( re.test(numeric)){ chaine = "0000" + numeric; }

   re = /^[1-9][0-9],[0-9][0-9]$/
   if ( re.test(numeric)){ chaine = "000" + numeric; }

   re = /^[1-9][0-9][0-9],[0-9][0-9]$/
   if ( re.test(numeric)){ chaine = "00" + numeric; }
   
   return chaine.replace(",","");

}

function config_prelev_type(type){
	prelev_type=type;
}

function send_request ( json_chaine )
{
	send=false;
	if (prelev_type=="actif")
	{
		target_page = "Prelevement-Actif.aspx"
		send=true;
		
	}
	else if (prelev_type=="retraite")
	{
		target_page = "Prelevement-Retraite.aspx"
		send=true;
	}
	else
	{
		alert("Le type de prelevement doit etre selectionne" + document.saisie_prelev.prelev_type.value)
		send=false;
		//break;
	}
	
	if (send) {
		location.href= target_page + "?input=" + json_chaine
	}
}
